#include "TopDownView.h"

int TileWidth, TileHeight, AreaWidth, AreaHeight, MapWidth, MapHeight;
int AvatarX, AvatarY; SDL_Surface *AvatarImage;

SDL_Surface *DisplayArea;

SDL_Surface **TileArray;
int TileArraySize;

SDL_Surface *AvatarImages[8];

int *DisplayMap;

void TDV_SetTileWidth(int Width)
{
	TileWidth = Width;
}
void TDV_SetTileHeight(int Height)
{
	TileHeight = Height;
}
void TDV_SetTileDimensions(int Width, int Height)
{
	TileWidth = Width;
	TileHeight = Height;
}
void TDV_ChangeAreaWidth(int Width)
{
	AreaWidth = Width;
}
void TDV_ChangeAreaHeight(int Height)
{
	AreaHeight = Height;
}
void TDV_ChangeAreaDimensions(int Width, int Height)
{
	AreaWidth = Width;
	AreaHeight = Height;
}
void TDV_SetAvatarX(int X)
{
	AvatarX = X;
}
void TDV_SetAvatarY(int Y)
{
	AvatarY = Y;
}
void TDV_SetAvatarPosition(int X, int Y)
{
	AvatarX = X;
	AvatarY = Y;
}
void TDV_SetMapWidth(int Width)
{
	MapWidth = Width;
}
void TDV_SetMapHeight(int Height)
{
	MapHeight = Height;
}
void TDV_SetMapDimensions(int Width, int Height)
{
	MapWidth = Width;
	MapHeight = Height;
}
int TDV_GetTileWidth()
{
	return TileWidth;
}
int TDV_GetTileHeight()
{
	return TileHeight;
}
int TDV_GetAreaWidth()
{
	return AreaWidth;
}
int TDV_GetAreaHeight()
{
	return AreaHeight;
}
int TDV_GetAvatarX()
{
	return AvatarX;
}
int TDV_GetAvatarY()
{
	return AvatarY;
}
int TDV_GetMapHeight()
{
	return MapHeight;
}
int TDV_GetMapWidth()
{
	return MapWidth;
}

int TDV_InitTopDownView(int Width, int Height, SDL_Surface *Display)
{
	/* Initialise the things needed for use in this interface */
	AreaWidth = Width;
	AreaHeight = Height;
	DisplayArea = Display;
	TileArray = malloc(10*sizeof(TileArray));
	if(TileArray == NULL) return -1;
	TileArraySize = 10;
	return 0;
}

int TDV_SetTileFromFile(int Reference, char *Filename)
{
	if(Reference >= TileArraySize)
	{
		TileArraySize += TileArraySize+Reference;
		void *TmpPointer = realloc(TileArray, TileArraySize*sizeof(TileArray));
		if(TmpPointer == NULL) return -1;
		TileArray = TmpPointer;
	}
	/* Getting a casting error when setting TileArray[Reference] = IMG_Load(..);
		Changed it for this which works */
	SDL_Surface *Tmp = IMG_Load(Filename);
	if(Tmp == NULL) return -1;
	TileArray[Reference] = Tmp;
	return Reference;
}


int TDV_SetTileFromSurface(int Reference, SDL_Surface *Surface)
{
	if(Reference >= TileArraySize)	
	{
		TileArraySize += TileArraySize + Reference;
		void *TmpPointer = realloc(TileArray, TileArraySize*sizeof(TileArray));
		if(TmpPointer == NULL) return -1;
		TileArray = TmpPointer;
	}
	TileArray[Reference] = Surface;
	return Reference;
}

int TDV_SetAvatarImage(SDL_Surface *Image)
{
	AvatarImage = Image;
	return 0;
}


void TDV_DrawToArea()
{
	/* Put the background of the area as black */
	SDL_Rect R;
	R.x = 0;
	R.y = 0;
	R.w = AreaWidth;
	R.h = AreaHeight;
	SDL_FillRect(DisplayArea, &R, 0x000000);
	
	/* Work out dimensions and whatnot */
	int TilesPerRow = AreaWidth/TileWidth;
	int TilesPerColumn = AreaHeight/TileHeight;
	int X, Y;
	int MapXUpper = AvatarX + (TilesPerRow/2);
	int MapXLower = AvatarX - (TilesPerRow/2);
	int MapYUpper = AvatarY + (TilesPerColumn/2);
	int MapYLower = AvatarY - (TilesPerColumn/2);
	/* Actually draw it */
	for(X = MapXLower; X < MapXUpper; ++X)
	{	
		for(Y = MapYLower; Y < MapYUpper; ++Y)
		{
			SDL_Surface *CurrentTile = TDV_I_GetTileAt(X, Y);
			if(CurrentTile != NULL) /* If the tile actually exists */
			{
				R.x = TileWidth*(X-MapXLower);
				R.y = TileHeight*(Y-MapYLower);
				R.w = TileWidth;
				SDL_BlitSurface(CurrentTile, NULL, DisplayArea, &R);
			}
		}
	}
}

SDL_Surface *TDV_I_GetTileAt(int X, int Y)
{
	/* Check the bounds */
	if(X < 0 || X >= MapWidth)
		return NULL;
	if(Y < 0 || Y >= MapHeight)
		return NULL;
	return TileArray[DisplayMap[X*MapHeight + Y]];
}

void TDV_SetMap(int *Map)
{
	DisplayMap = Map;
}

void TDV_DrawAvatar()
{
	/* Draw the player image on to the middle of the area */
	SDL_Rect R;
	R.x = AreaWidth/2;
	R.y = AreaHeight/2;
	SDL_BlitSurface(AvatarImage, NULL, DisplayArea, &R);
}
